package com.one.square.file.server.config;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.one.square.core.entity.FileInfo;
import com.one.square.file.server.mapper.FileMapper;
import com.one.square.file.server.service.IFileService;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author cwm
 * @Description 定时清理无效资源
 * @date 2021/10/29 下午2:10
 * @Version 1.0
 */
@Component
@AllArgsConstructor
public class ScheduledService {
    private final IFileService iFileService;
    private final FileMapper fileMapper;

    @Scheduled(cron = "0 0 23 1/1 * ? ")
    @Async
    public void cleanFile(){
        //查询无效文件
        List<FileInfo> fileInfos = fileMapper.selectList(Wrappers.<FileInfo>lambdaQuery().eq(FileInfo::getType, Boolean.FALSE).select(FileInfo::getId, FileInfo::getPath));
        if (CollectionUtil.isNotEmpty(fileInfos)){
            for (FileInfo fileInfo:fileInfos
                 ) {
                iFileService.delete(fileInfo.getPath());
                fileMapper.deleteById(fileInfo.getId());
            }
        }
    }

}
